package com.ys.controller.web.business;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.entity.business.*;
import com.ys.entity.storage.TccWarehouse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.constant.AppConstant;
import com.ys.entity.storage.TccAllocateDetail;
import com.ys.entity.storage.TccStockControl;
import com.ys.entity.sys.TsUserInfo;
import com.ys.enume.business.TpProductMaterialEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.ImgcodeService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 精品库页面
 */
@Controller
@RequestMapping("/web/business/tpBoutique")
public class WebTpBoutiqueController {
	private static final Logger logger = LoggerFactory.getLogger(WebTpBoutiqueController.class);

	@Autowired
	private ImgcodeService imgcodeService;
	@Autowired
	private CommonApiService<TpBoutique> commonApiService;
	


	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		TpBoutique entity = new TpBoutique().setId(id).queryFirst();
		try {
			commonApiService.toEdit(model,id, new TpBoutique(), MenuId.TP_Boutique, null, null);
			List<Map<String, Object>> tpBoutiqueSiteDetailArr =getBoutiqueSiteDetail(id);
			List<Map<String, Object>> tpBoutiqueAuthList = getBoutiqueAuth(entity.getCode());

					// 调货详情数组
			List<TpBoutiqueDetailEdit.DetailDto> settingDetailArr = new ArrayList<TpBoutiqueDetailEdit.DetailDto>();
			String pcode = entity.getCode();
			List<TpBoutiqueGoods> listProductCarefullySetting = new TpBoutiqueGoods().setPcode(pcode).queryList();
			if (listProductCarefullySetting != null) {
				for(TpBoutiqueGoods tpBoutiqueGoods : listProductCarefullySetting) {
					Long id2 = tpBoutiqueGoods.getId();
					String code2 = tpBoutiqueGoods.getCode();
					String attr = tpBoutiqueGoods.getAttr();
					String warehouse = tpBoutiqueGoods.getWarehouse();
					String size = tpBoutiqueGoods.getSize();
					String weight = tpBoutiqueGoods.getWeight();
					BigDecimal retailPrice = tpBoutiqueGoods.getRetailPrice();
					BigDecimal originPrice = tpBoutiqueGoods.getOriginPrice();
					Integer numPerBox = tpBoutiqueGoods.getNumPerBox();
					Integer numBuyMin = tpBoutiqueGoods.getNumBuyMin();
					Integer numBuyMinSecond = tpBoutiqueGoods.getNumBuyMinSecond();
					String unitName = tpBoutiqueGoods.getUnitName();
					Integer period = tpBoutiqueGoods.getPeriod();
					String timeArrivePredict = tpBoutiqueGoods.getTimeArrivePredict();
					Integer numAll = tpBoutiqueGoods.getNumAll();
					Integer numPreBuy = tpBoutiqueGoods.getNumPreBuy();
					settingDetailArr.add(new TpBoutiqueDetailEdit.DetailDto(id2, code2, pcode, attr, warehouse, size, weight, retailPrice, originPrice, numPerBox, numBuyMin, numBuyMinSecond, unitName, period, timeArrivePredict, numAll, numPreBuy));
				}
			}
			TpBoutiqueDetailEdit detailList = new TpBoutiqueDetailEdit(id,entity.getCode(), tpBoutiqueSiteDetailArr, settingDetailArr,tpBoutiqueAuthList);
			model.addAttribute("detail", detailList);
			model.addAttribute("onProcess", 0);
			return "business/tpBoutique/tpBoutiqueEdit";
		}catch (Exception e) {
			logger.error("精品库表编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


	//获取站点详情数据
	public List<Map<String, Object>> getBoutiqueAuth(String code){
		List<Map<String, Object>> tpBoutiqueAuthList = new TpBoutiqueAuth()
				.addLeftJoin("THR_OperateCompanyApply C ON C.Vgsbm = A.Vgsbm")
				.addField("C.Vgsjc AS vgsjc")
				.setBoutiqueCode(code)
				.queryMap();

		return tpBoutiqueAuthList;
	}

	//获取站点详情数据
	public List<Map<String, Object>> getBoutiqueSiteDetail(Long id){
		List<Map<String, Object>> tpBoutiqueSiteDetailArr = new TpBoutiqueSiteDetail()
				.addField("B.Code AS code")
				.addField("L.CategoryPath AS baseinfoLmName")

				.addField("D1.NameUnitEn AS normsUnit")
				.addField("D2.NameUnitEn AS weightUnit")
				.addLeftJoin("TP_Boutique B ON B.Id=A.BoutiqueId")
				.addLeftJoin("TB_BaseinfoLmwh L ON A.CommodityLmId = L.Id AND L.Status!=0")

				.addLeftJoin("TB_BaseinfoDwmc D1 ON A.NormsUnitId = D1.Id AND D1.Status!=0")
				.addLeftJoin("TB_BaseinfoDwmc D2 ON A.WeightUnitId = D2.Id AND D2.Status!=0")

                /*
                .addLeftJoin("TP_AmazonRegionItem I ON A.AmazonRegionItemId = I.Id")
                .addLeftJoin("TP_AmazonRegion R ON I.RegionCode = R.Code")
                .addField("I.CountryName AS countryName")//站点
                .addField("R.Name AS regionName")//区域
                .addField("R.Platform AS platform")//平台
                .addField("I.Id AS itemId")//站点id

                 */

                .addLeftJoin("TB_Site S ON S.Id = A.SiteId")
                .addField("S.Id AS itemId")//站点id
                .addLeftJoin("TP_AmazonRegion AR ON AR.Code = S.RegionCode")
                .addField("AR.Name regionName")
                .addLeftJoin("TB_Platform B1 ON B1.Code = AR.PlatformCode")
                .addField("B1.Code platformCode")
                .addField("B1.Name platformName")
                .addLeftJoin("TB_Country C ON C.Id = S.CountryId")
                .addField("C.Name countryName")



				.setBoutiqueId(id)
				.queryMap();


		return tpBoutiqueSiteDetailArr;
	}



	/*
	 * 详情
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		TpBoutique entity = new TpBoutique().setId(id).queryFirst();
		try {
			commonApiService.toEdit(model,id, new TpBoutique(), MenuId.TP_Boutique_Detail, null, null);

			List<Map<String, Object>> tpBoutiqueSiteDetailArr =getBoutiqueSiteDetail(id);
			List<Map<String, Object>> tpBoutiqueAuthList = getBoutiqueAuth(entity.getCode());

			// 调货详情数组
			List<TpBoutiqueDetailEdit.DetailDto> settingDetailArr = new ArrayList<TpBoutiqueDetailEdit.DetailDto>();
			String pcode = entity.getCode();
			List<TpBoutiqueGoods> listProductCarefullySetting = new TpBoutiqueGoods().setPcode(pcode).queryList();
			if (listProductCarefullySetting != null) {
				for(TpBoutiqueGoods productCarefullySetting : listProductCarefullySetting) {
					Long id2 = productCarefullySetting.getId();
					String code2 = productCarefullySetting.getCode();
					String attr = productCarefullySetting.getAttr();
					String warehouse = productCarefullySetting.getWarehouse();
					String size = productCarefullySetting.getSize();
					String weight = productCarefullySetting.getWeight();
					BigDecimal retailPrice = productCarefullySetting.getRetailPrice();
					BigDecimal originPrice = productCarefullySetting.getOriginPrice();
					Integer numPerBox = productCarefullySetting.getNumPerBox();
					Integer numBuyMin = productCarefullySetting.getNumBuyMin();
					Integer numBuyMinSecond = productCarefullySetting.getNumBuyMinSecond();
					String unitName = productCarefullySetting.getUnitName();
					Integer period = productCarefullySetting.getPeriod();
					String timeArrivePredict = productCarefullySetting.getTimeArrivePredict();
					Integer numAll = productCarefullySetting.getNumAll();
					Integer numPreBuy = productCarefullySetting.getNumPreBuy();
					settingDetailArr.add(new TpBoutiqueDetailEdit.DetailDto(id2, code2, pcode, attr, warehouse, size, weight, retailPrice, originPrice, numPerBox, numBuyMin, numBuyMinSecond, unitName, period, timeArrivePredict, numAll, numPreBuy));
				}
			}
			TpBoutiqueDetailEdit detailList = new TpBoutiqueDetailEdit(id,entity.getCode(), tpBoutiqueSiteDetailArr, settingDetailArr,tpBoutiqueAuthList);
			model.addAttribute("detail", detailList);
			model.addAttribute("onProcess", 1);
			return "business/tpBoutique/tpBoutiqueEdit";
		}catch (Exception e) {
			logger.error("精品库表详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


	@Data
	@AllArgsConstructor
	private static class TpBoutiqueDetailEdit{
		private Long id;
		private String code;
		private List<Map<String, Object>> tpBoutiqueSiteDetailArr; // 站点详情数据
		private List<DetailDto> settingDetailArr; // 调货详情数据
		private List<Map<String, Object>> tpBoutiqueAuthList; // 权限信息数据

		@Data
		@AllArgsConstructor
		public static class DetailDto {
			private Long id; // 
			private String code; // 商品编码
			private String pcode; // 所属精品编码
			private String attr; // 属性名
			private String warehouse; // 所在仓库
			private String size; // 箱规尺寸(cm)
			private String weight; // 箱规重量(kg)
			private BigDecimal retailPrice; // 建议售价
			private BigDecimal originPrice; // 单价
			private Integer numPerBox; // 单箱数量
			private Integer numBuyMin; // 首次起订箱数
			private Integer numBuyMinSecond; // 再次起订箱数
			private String unitName; // 单位名称
			private Integer period; // 生产货期
			private String timeArrivePredict; // 预计到货时间
			private Integer numAll; // 发货数量
			private Integer numPreBuy; // 预购数量
		}
	}

	/*
	 * 逸思精品首页
	 */
	@GetMapping("/index")
	public String index(Model model, String loginName, String pass){

		String loginUserId = ShiroUtils.getUserId();
		if (loginUserId == null || loginUserId.length() == 0) {
			if (loginName != null && loginName.length() > 0 && pass != null && pass.length() > 0) {

				TsUserInfo tsUserInfo = new TsUserInfo().setUserId(loginName).setUserPassWord(pass).queryFirst();
				if (tsUserInfo != null) {
					imgcodeService.doAuthAfterLogin(tsUserInfo);
				}
			} else {
				return "/login";
			}
		} else {
			if (loginName != null && loginName.length() > 0 && pass != null && pass.length() > 0 && !loginUserId.equals(loginName)) {

				TsUserInfo tsUserInfo = new TsUserInfo().setUserId(loginName).setUserPassWord(pass).queryFirst();
				if (tsUserInfo != null) {
					imgcodeService.doAuthAfterLogin(tsUserInfo);
				}
			}
		}
		
		Integer numProductOnCart = new TpBoutiqueCart().setCreateUserId(loginUserId).count();
		IndexDto dto = new IndexDto(numProductOnCart);
		model.addAttribute("dto", dto);
		return "business/tpBoutique/index";
	}
	@Data
	@AllArgsConstructor
	private static class IndexDto{
		private Integer numProductOnCart; // 购物车条数
	}
	
	/*
	 * 商品详细页面
	 */
	@GetMapping("/toBuy")
	public String toBuy(Model model, String code){
		
		if (code == null || code.length() == 0) {
			return W.error(model, "无效的来源");
		}
		
		TpBoutique tpBoutique = new TpBoutique().setCode(code).queryFirst();
		if (tpBoutique == null) {
			return W.error(model, "商品信息有误");
		}
		
		TpBoutiqueSiteDetail tpBoutiqueSiteDetail = new TpBoutiqueSiteDetail()
				.setBoutiqueId(tpBoutique.getId())
				//.setAmazonRegionItemId(15L)
				.setOrderby("A.Id ASC")
				.queryFirst();
		if (tpBoutiqueSiteDetail == null) {
			return W.error(model, "商品站点信息有误");
		}
		
		
		// 价格数组-用于显示
		List<ToBuyDto.PriceDto> priceArr = new ArrayList<ToBuyDto.PriceDto>();
		// 价格数组-用于计算
		List<ToBuyDto.PriceDto2> prices = new ArrayList<ToBuyDto.PriceDto2>();
		List<TpBoutiqueDiscount> listTysProductDiscount = new TpBoutiqueDiscount().queryList();
		{
			BigDecimal amount = BigDecimal.ZERO;
			BigDecimal discount = BigDecimal.ONE;
			String condText = "任意数量";
			String percentText = "¥" + tpBoutique.getOriginPriceRange();
			BigDecimal percent = discount;
			
			priceArr.add(new ToBuyDto.PriceDto(condText, percentText));
			prices.add(new ToBuyDto.PriceDto2(amount, percent));
		}
		for(TpBoutiqueDiscount tysProductDiscount : listTysProductDiscount) {
			
			BigDecimal amount = tysProductDiscount.getAmount();
			BigDecimal discount = tysProductDiscount.getDiscount();
			String condText = "满" + amount;
			String percentText;
			percentText = discount.multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).toString() + "%";
			BigDecimal percent = discount;
			
			priceArr.add(new ToBuyDto.PriceDto(condText, percentText));
			prices.add(new ToBuyDto.PriceDto2(amount, percent));
		}
		
		// 合并商品数组
		List<ToBuyDto.MergeDto> mergeArr = new ArrayList<ToBuyDto.MergeDto>();
		List<TpBoutique> listTpBoutique = new TpBoutique().setCodeMerge(tpBoutique.getCodeMerge()).queryList();
		for(TpBoutique tpBoutiqueMerge : listTpBoutique) {
			
			TpProductActive active = new TpProductActive().setCode(tpBoutiqueMerge.getCode()).queryFirst();
			Long id = tpBoutiqueMerge.getId();
			String code2 = tpBoutiqueMerge.getCode();
			String img = active.getImg();
			String link = "/web/business/tpBoutique/toBuy?code=" + tpBoutiqueMerge.getCode();
			boolean isSelected = tpBoutiqueMerge.getCode().equals(tpBoutique.getCode());
			mergeArr.add(new ToBuyDto.MergeDto(id, code2, img, link, isSelected));
		}
		
		// 调拨详情数组
		List<ToBuyDto.DetailDto> detailArr = new ArrayList<ToBuyDto.DetailDto>();
		String pcode = code;
		List<TpBoutiqueGoods> listProductCarefullySetting = new TpBoutiqueGoods().setPcode(code).queryList();
		for(TpBoutiqueGoods tpBoutiqueGoods : listProductCarefullySetting) {
			Long id = tpBoutiqueGoods.getId();
			String code2 = tpBoutiqueGoods.getCode();
			String attr = tpBoutiqueGoods.getAttr();
			String warehouse = tpBoutiqueGoods.getWarehouse();
			String size = tpBoutiqueGoods.getSize();
			String weight = tpBoutiqueGoods.getWeight();
			BigDecimal retailPrice = tpBoutiqueGoods.getRetailPrice();
			BigDecimal originPrice = tpBoutiqueGoods.getOriginPrice();
			String unitName = tpBoutiqueGoods.getUnitName();
			Integer numPerBox = tpBoutiqueGoods.getNumPerBox();
			// 起订箱数根据首次或再次，进行判断
			Integer numBuyMin = tpBoutiqueGoods.getNumBuyMin();
			BigDecimal numAllotSelf = BigDecimal.ZERO;
//			BigDecimal numAllotSelf = stockAllotMapper.sumAllotAllByCodeAndUserId(code2, ShiroUtils.getUserId());
			if (numAllotSelf.compareTo(BigDecimal.ZERO) > 0) {
				numBuyMin = tpBoutiqueGoods.getNumBuyMinSecond();
			}
			
			/*
			 * 库存数量计算规则
			 * 只统计精品3区的GS0641名下库存数量
			 */
			Integer stock = new TccStockControl()
					.setProductCode(tpBoutiqueGoods.getCode())
					.setVgsbm(AppConstant.BOUTIQUE_VGSBM)
					.setGdepot(tpBoutiqueGoods.getWarehouse())
					.setFields("(ISNULL(SUM(A.ProductNum), 0))").sumDecimal().intValue();
			String numAllot = "";
			String realPrice = "";
			String totalPrice = "";
			detailArr.add(new ToBuyDto.DetailDto(id, code2, pcode, attr, warehouse, size, weight, retailPrice, originPrice, unitName, numPerBox, numBuyMin, stock, numAllot, realPrice, totalPrice));
		}
		
		// 图片数组
		List<String> imgArr1 = new ArrayList<String>();
		{
			imgArr1.add(tpBoutiqueSiteDetail.getMainPic2());
			imgArr1.add(tpBoutiqueSiteDetail.getMainPic3());
			imgArr1.add(tpBoutiqueSiteDetail.getSizePic1());
			imgArr1.add(tpBoutiqueSiteDetail.getScenePic1());
		}
		
		// 图片数组
		List<String> imgArr2 = new ArrayList<String>();
		{
			imgArr2.add(tpBoutiqueSiteDetail.getScenePic2());
			imgArr2.add(tpBoutiqueSiteDetail.getDetailsPic1());
			imgArr2.add(tpBoutiqueSiteDetail.getUsePic1());
			imgArr2.add(tpBoutiqueSiteDetail.getUsePic2());
		}

		TpProductBasis productBasis = new TpProductBasis().setCode(code).queryFirst();
		
		if (productBasis == null) {
			return W.error(model, "对应的商品库数据不存在，请确认数据来源");
		}
		String img = tpBoutiqueSiteDetail.getMainPic1();
		String name = tpBoutique.getName();
		Integer numAllot = new TccAllocateDetail().setProductCode(code).setFields("(ISNULL(SUM(A.Num), 0))").sumDecimal().intValue();
		
		// 商品材质
		String material = "";
		List<TpProductMaterial> listTpProductMaterial = new TpProductMaterial().setProductCode(code).setType(TpProductMaterialEnum.Type.TP_ProductActive.value).queryList();
		for(TpProductMaterial tpProductMaterial : listTpProductMaterial) {
			material += tpProductMaterial.getMaterialNameCn() + ",";
		}
		material = material.length() > 0 ? material.substring(0, material.length() - 1) : "--";
		
//		String material = productBasis.getMaterialCn();
		String size = productBasis.getSizeLength() + "*" + productBasis.getSizeWidth() + "*" + productBasis.getSizeHeight();
		String weight = productBasis.getSizeWeight() + "";
		String linkJpUk = tpBoutique.getLinkJpUk() != null && tpBoutique.getLinkJpUk().length() > 0 ? tpBoutique.getLinkJpUk() : "不可售";
		String linkJpUs = tpBoutique.getLinkJpUs() != null && tpBoutique.getLinkJpUs().length() > 0 ? tpBoutique.getLinkJpUs() : "不可售";
		String linkVideo = tpBoutique.getVideolink();
		Integer type = tpBoutique.getType();
		ToBuyDto.ProfitDto usa = null;
		{
			BigDecimal retailPrice = tpBoutique.getRetailPriceUsa();
			String floorPrice = tpBoutique.getFloorPriceUsa() !=null ? tpBoutique.getFloorPriceUsa().toString() : "0";
			String cost = tpBoutique.getCostUsa() != null ? tpBoutique.getCostUsa().toString() : "0";
			String grossMargin = tpBoutique.getGrossMarginUsa() != null ? tpBoutique.getGrossMarginUsa().toString() : "0";
			String capitalOutoutRatio = tpBoutique.getCapitalOutoutRatioUsa() != null ? tpBoutique.getCapitalOutoutRatioUsa().toString() : "0";
			String deliveryFee = tpBoutique.getDeliveryFeeUsa().toString();
			usa = new ToBuyDto.ProfitDto(retailPrice, floorPrice, cost, grossMargin, capitalOutoutRatio, deliveryFee);
		}
		ToBuyDto.ProfitDto uk = null;
		{
			BigDecimal retailPrice = tpBoutique.getRetailPriceUk();
			String floorPrice = tpBoutique.getFloorPriceUk() !=null ? tpBoutique.getFloorPriceUk().toString() : "0";
			String cost = tpBoutique.getCostUk() != null ? tpBoutique.getCostUk().toString() : "0";
			String grossMargin = tpBoutique.getGrossMarginUk() != null ? tpBoutique.getGrossMarginUk().toString() : "0";
			String capitalOutoutRatio = tpBoutique.getCapitalOutoutRatioUk() != null ? tpBoutique.getCapitalOutoutRatioUk().toString() : "0";
			String deliveryFee = tpBoutique.getDeliveryFeeUk().toString();
			uk = new ToBuyDto.ProfitDto(retailPrice, floorPrice, cost, grossMargin, capitalOutoutRatio, deliveryFee);
		}
		
		String remark = tpBoutique.getRemark();
		
		String sizePackage = tpBoutique.getSizePackage();
		String weightPackage = tpBoutique.getWeightPackage();
		
		String advantage = tpBoutique.getAdvantage();
		
		// 统计购物车条数
		String loginUserId = ShiroUtils.getUserId();
		Integer numProductOnCart = new TpBoutiqueCart().setCreateUserId(loginUserId).count();
		Long id = tpBoutique.getId();
		ToBuyDto dto = new ToBuyDto(id, img, name, code, numAllot, material, size, weight, linkJpUk, linkJpUs, linkVideo, type,
				remark, sizePackage, weightPackage,
				usa, uk, priceArr, prices, detailArr, mergeArr, imgArr1, imgArr2, advantage, numProductOnCart);
		
		model.addAttribute("dto", dto);
		return "business/tpBoutique/toBuy";
	}
	@Data
	@AllArgsConstructor
	public static class ToBuyDto {
		private Long id;
		private String img; // 主图1
		private String name; // 商品名称
		private String code; // 商品编码
		private Integer numAllot; // 历史调拨量
		private String material; // 商品材质
		private String size; // 商品尺寸(cm)，如：15.2*9.7*7
		private String weight; // 商品重量(g)
		private String linkJpUk; // 竞品链接(英国)
		private String linkJpUs; // 竞品链接(美国)
		private String linkVideo; // 视频链接
		private Integer type; // 类型，如：新品/爆品
		private String remark; // 备注
		private String sizePackage; // 
		private String weightPackage; // 包装重量f
		private ProfitDto usa; // 美国建议售价最低售价及其分析
		private ProfitDto uk; // 英国建议售价最低售价及其分析
		private List<PriceDto> priceArr; // 价格数组，至少一个，最多3个，若不足3个则需补齐3个
		private List<PriceDto2> prices; // 价格数组，用于计算调拨单价及总价
		private List<DetailDto> detailArr;
		private List<MergeDto> mergeArr; // 合并商品数组
		private List<String> imgArr1; // 图片数组1，主图2，主图3，尺寸图1，场景图1
		private List<String> imgArr2; // 图片数组2，场景图2，细节图1，应用图1，应用图2
		private String advantage; // 商品竞争优势
		
		private Integer numProductOnCart; // 购物车条数

		@Data
		@AllArgsConstructor
		public static class PriceDto {
			private String condText; // 条件文本，如：满200
			private String percentText; //  折扣，如：98%
		}

		@Data
		@AllArgsConstructor
		public static class PriceDto2 {
			private BigDecimal amount; // 总金额
			private BigDecimal percent; //  如：0.98
		}

		@Data
		@AllArgsConstructor
		public static class ProfitDto {
			private BigDecimal retailPrice; // 建议售价
			private String floorPrice; // 最低售价
			private String cost; // 成本
			private String grossMargin; // 预估毛利率
			private String capitalOutoutRatio; // 预估毛利
			private String deliveryFee; // FBA配送费
		}

		@Data
		@AllArgsConstructor
		public static class DetailDto {
			private Long id; // 
			private String code; // 商品编码
			private String pcode; // 所属精品编码
			private String attr; // 属性名
			private String warehouse; // 仓库
			private String size; // 箱规尺寸(cm)
			private String weight; // 箱规重量(kg)
			private BigDecimal retailPrice; // 建议售价
			private BigDecimal originPrice; // 单价
			private String unitName; // 单位名称
			private Integer numPerBox; // 单箱数量
			private Integer numBuyMin; // 起订箱数
			private Integer stock; // 库存
			private String numAllot; // 调拨数量
			private String realPrice; // 调拨单价
			private String totalPrice; // 调拨总价
		}

		@Data
		@AllArgsConstructor
		public static class MergeDto {
			private Long id; // 
			private String code; // 商品编码
			private String img; // 商品图片
			private String link; // 链接
			private boolean isSelected; // 是否选中
		}
	}

	/*
	 * 购物车页面
	 */
	@GetMapping("/toCart")
	public String toCart(Model model){
		
		List<ToCartDto.DetailDto> detailArr = new ArrayList<ToCartDto.DetailDto>();
		String loginUserId = ShiroUtils.getUserId();
		List<TpBoutiqueCart> listTysCart = new TpBoutiqueCart().setCreateUserId(loginUserId).queryList();
		for(TpBoutiqueCart entity : listTysCart) {
			TpBoutiqueGoods tpBoutiqueGoods = new TpBoutiqueGoods().setId(entity.getBoutiqueGoodsId()).queryFirst();
			Long id = entity.getId();
			Long boutiqueGoodsId = entity.getBoutiqueGoodsId();
			String productCode = entity.getProductCode();
			String productName = entity.getProductName();
			String attr = tpBoutiqueGoods.getAttr();
//			String depotId = new TsDepotInfo().setDepotName(tpBoutiqueGoods.getWarehouse()).queryFirst().getDepotId();
			//修改到1.5的仓库
			String depotId = new TccWarehouse().setVck(tpBoutiqueGoods.getWarehouse()).queryFirst().getCck();
			String warehouse = tpBoutiqueGoods.getWarehouse();
			Integer numPerBox = entity.getNumPerBox();
			Integer numBuyMin = entity.getNumBuyMin();
			Integer numBox = entity.getNumBox();
			Integer numAllot = entity.getNumAllot();
			BigDecimal originPrice = entity.getOriginPrice();
			
			Integer stock = new TccStockControl()
					.setProductCode(entity.getProductCode())
					.setVgsbm(AppConstant.BOUTIQUE_VGSBM)
					.setGdepot(tpBoutiqueGoods.getWarehouse())
					.setFields("(ISNULL(SUM(A.ProductNum), 0))").sumDecimal().intValue();
			BigDecimal realPrice = BigDecimal.ZERO;
			BigDecimal totalPrice = BigDecimal.ZERO;
			detailArr.add(new ToCartDto.DetailDto(id, boutiqueGoodsId, productCode, productName, attr, depotId, warehouse, numPerBox, numBuyMin, numBox, numAllot, originPrice, stock, realPrice, totalPrice));
		}
		
		// 折扣数组
		List<ToCartDto.DiscountDto> discountArr = new ArrayList<ToCartDto.DiscountDto>();
		{
			BigDecimal amount = BigDecimal.ZERO;
			BigDecimal discount = BigDecimal.ONE;
			discountArr.add(new ToCartDto.DiscountDto(amount, discount));
		}
		List<TpBoutiqueDiscount> listTysProductDiscount = new TpBoutiqueDiscount().queryList();
		for(TpBoutiqueDiscount tysProductDiscount : listTysProductDiscount) {
			BigDecimal amount = tysProductDiscount.getAmount();
			BigDecimal discount = tysProductDiscount.getDiscount();
			discountArr.add(new ToCartDto.DiscountDto(amount, discount));
		}
		
		/*
		 *  价格信息数组
		 */
		List<ToCartDto.PriceInfoDto> priceInfoArr = new ArrayList<ToCartDto.PriceInfoDto>();
		// 原价
		{
			String title = "原价";
			String subTitle = "无限制";
			priceInfoArr.add(new ToCartDto.PriceInfoDto(title, subTitle));
		}
		for(TpBoutiqueDiscount tysProductDiscount : listTysProductDiscount) {
			String title = tysProductDiscount.getDiscount().multiply(new BigDecimal(100)).setScale(0) + "%";
			String subTitle = "满" + tysProductDiscount.getAmount().setScale(0, BigDecimal.ROUND_HALF_UP);
			priceInfoArr.add(new ToCartDto.PriceInfoDto(title, subTitle));
		}
		
		BigDecimal totalPrice = BigDecimal.ZERO;
		Integer totalNum = 1;
		ToCartDto dto = new ToCartDto(totalPrice, totalNum, detailArr, discountArr, priceInfoArr);
		
		model.addAttribute("dto", dto);
		return "business/tpBoutique/toCart";
	}
	@Data
	@AllArgsConstructor
	public static class ToCartDto {
		private BigDecimal totalPrice; //  
		private Integer totalNum; //  
		private List<DetailDto> detailArr;
		private List<DiscountDto> discountArr;
		private List<PriceInfoDto> priceInfoArr;

		@Data
		@AllArgsConstructor
		public static class DetailDto {
			private Long id; // 
			private Long boutiqueGoodsId; // 精品ID
			private String productCode; // 商品编码
			private String productName; // 商品编码
			private String attr; // 
			private String depotId; // 仓库编码
			private String warehouse; // 仓库
			private Integer numPerBox; // 单箱数量
			private Integer numBuyMin; // 起订箱数
			private Integer numBox;
			private Integer numAllot; // 调拨数量
			private BigDecimal originPrice; // 单价
			private Integer stock; // 库存
			private BigDecimal realPrice; // 调拨单价
			private BigDecimal totalPrice; // 调拨总价
		}

		@Data
		@AllArgsConstructor
		public static class DiscountDto {
			private BigDecimal amount; // 总金额
			private BigDecimal discount; //  如：0.98
		}

		@Data
		@AllArgsConstructor
		public static class PriceInfoDto {
			private String title; // 标题，如：¥16
			private String subTitle; //  副标题，如：满20000
		}
	}
}






